package acm.pta5;

import 左哥算法.compare.二叉树.ch01二叉树的结构;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class T3_1 {
    static class Tree {
        int val;
        Tree parent;
        Tree left;
        Tree right;

        public Tree(int val) {
            this.val = val;
        }

        public Tree findParent(int val) {
            Tree curr = this;
            Tree parent = null;
            while (curr != null) {
                if (curr.val == val) {
                    return parent;
                }
                parent = curr;
                curr = val < curr.val ? curr.left : curr.right;
            }
            return parent;
        }

        public Tree find(int val) {
            Tree curr = this;
            while (curr != null) {
                if (curr.val == val) {
                    return curr;
                }
                curr = val < curr.val ? curr.left : curr.right;
            }
            return curr;
        }

        public void insert(int val) {
            Tree parent = findParent(val);
            Tree node = new Tree(val);
            if (val < parent.val) {
                parent.left = node;
            } else {
                parent.right = node;
            }
            node.parent = parent;
        }

        public static Tree build(String[] arr) {
            if (arr.length == 0) return null;
            Tree root = new Tree(Integer.parseInt(arr[0]));
            for (int i = 1; i < arr.length; i++) {
                root.insert(Integer.parseInt(arr[i]));
            }
            return root;
        }

        public boolean isRoot(int val) {
            return this.val == val;
        }

        /**
         * 找到其父节点
         * 而后比对其另一个节点
         */
        public boolean isBrother(int a, int b) {
            Tree at = find(a);
            Tree bt = find(b);
            return at != null && bt != null && at.parent == bt.parent;
        }

        public boolean isParent(int parent, int child) {
            Tree c = find(child);
            return c!=null&&c.parent!=null&&c.parent.val==parent;
        }

        public boolean isLeft(int parent, int left) {
            Tree p = findParent(left);
            return p != null && p.left != null && p.left.val == left && p.val == parent;
        }

        public boolean isRight(int parent, int right) {
            Tree p = findParent(right);
            return p != null && p.right != null && p.right.val == right && p.val == parent;
        }

        /**
         * 层序遍历，滚动边界分层
         */
        public boolean isLevel(int a, int b) {
            if (a==b){
                return find(a)!=null;
            }
            Tree currEnd = this, nextEnd = null;
            Queue<Tree> queue = new LinkedList<>();
            queue.add(this);
            boolean isFind = false;   //是否找到一个，换层时和找到节点时的终止条件，判断。
            while (!queue.isEmpty()) {
                Tree curr = queue.poll();
                if (curr.val == a || curr.val == b) {
                    if (isFind) {
                        return true;
                    } else {
                        isFind = true;
                    }
                }
                if (curr.left != null) {
                    nextEnd = curr.left;
                    queue.add(curr.left);
                }
                if (curr.right != null) {
                    nextEnd = curr.right;
                    queue.add(curr.right);
                }
                if (curr == currEnd) {
                    if (isFind) {    //当前层遍历完但只找到一个
                        return false;
                    }
                    currEnd = nextEnd;
                }
            }
            return false;
        }

        @Override
        public String toString() {
            return val + "";
        }
    }

    //    static {
//        String[] arr="2 4 1 3 0".split(" ");
////        String[] arr="2 4 1 3 0".split(" ");
//        Tree root = Tree.build(arr);
//        System.out.println(root.isParent(2,4));
//        System.out.println(root.isLevel(40,40));
//        System.out.println(root.isBrother(73,32));
//        System.out.println(root.isParent(61,37));
//        System.out.println(root.isRoot(2));
//        System.out.println(root.isBrother(1,4));
//        System.out.println(root.isLevel(3,0));
//        System.out.println(root.isParent(3,8));
//        System.out.println(root.isLeft(4,3));
//        System.out.println(root.isLevel(4,4));
//        System.out.println(root.isRight(3,100));
//    }
//    static {
//        String[] arr="88 42 11 12".split(" ");
//        Tree root = Tree.build(arr);
//        System.out.println(root.isLevel(12,12));
//        ch01二叉树的结构.compare(社交群体::fun);
//    }
//
//    public static StringBuilder fun(String tree,String[] conditions){
//        Tree root = Tree.build(tree.split(" "));
//        String[] read=null;
//        StringBuilder sb=new StringBuilder();
//        for (String condition : conditions) {
//            read=condition.split(" ");
//            switch (read[read.length-1]){
//                case "root":
//                    sb.append(root.isRoot(Integer.parseInt(read[0]))?"Yes":"No").append("\n");
//                    continue;
//                case "siblings":
//                    sb.append(root.isBrother(Integer.parseInt(read[0]),Integer.parseInt(read[2]))?"Yes":"No").append("\n");
//                    continue;
//                case "level":
//                    sb.append(root.isLevel(Integer.parseInt(read[0]),Integer.parseInt(read[2]))?"Yes":"No").append("\n");
//                    continue;
//            }
//            switch (read[read.length-4]){
//                case "the":
//                    sb.append(root.isParent(Integer.parseInt(read[0]),Integer.parseInt(read[read.length-1]))?"Yes":"No").append("\n");
//                    continue;
//                case "left":
//                    sb.append(root.isLeft(Integer.parseInt(read[read.length-1]),Integer.parseInt(read[0]))?"Yes":"No").append("\n");
//                    continue;
//                case "right":
//                    sb.append(root.isRight(Integer.parseInt(read[read.length-1]),Integer.parseInt(read[0]))?"Yes":"No").append("\n");
//                    continue;
//            }
//        }
//        return sb;
//    }



    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        in.readLine();
        Tree root = Tree.build(in.readLine().split(" "));
        int n = Integer.parseInt(in.readLine());
        String[] read = null;
        if (root == null) {
            while (n-- > 0) {
                System.out.println("No");
            }
            return;
        }
        while (n-- > 0) {
            read = in.readLine().split(" ");
            switch (read[read.length - 1]) {
                case "root":
                    System.out.println(root.isRoot(Integer.parseInt(read[0])) ? "Yes" : "No");
                    continue;
                case "siblings":
                    System.out.println(root.isBrother(Integer.parseInt(read[0]), Integer.parseInt(read[2])) ? "Yes" : "No");
                    continue;
                case "level":
                    System.out.println(root.isLevel(Integer.parseInt(read[0]), Integer.parseInt(read[2])) ? "Yes" : "No");
                    continue;
            }
            switch (read[read.length - 4]) {
                case "the":
                    System.out.println(root.isParent(Integer.parseInt(read[0]), Integer.parseInt(read[read.length - 1])) ? "Yes" : "No");
                    continue;
                case "left":
                    System.out.println(root.isLeft(Integer.parseInt(read[read.length - 1]), Integer.parseInt(read[0])) ? "Yes" : "No");
                    continue;
                case "right":
                    System.out.println(root.isRight(Integer.parseInt(read[read.length - 1]), Integer.parseInt(read[0])) ? "Yes" : "No");
                    continue;
            }
        }
    }
}
